package com.ccgk.logistics.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Company;
import com.ccgk.logistics.domain.Order;
import com.ccgk.logistics.domain.Port;
import com.ccgk.logistics.domain.User;
import com.ccgk.logistics.service.CompanyService;
import com.ccgk.logistics.service.OrderService;
import com.ccgk.logistics.service.PortService;
import com.ccgk.logistics.util.ToolUtil;

@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private PortService portService;
	
	@Autowired
	private CompanyService companyService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping("/add")
	public String toAddView(HttpServletRequest request){
		
		List<Port> listPort = portService.findAll();
		request.setAttribute("listPort",listPort);
		
		List<Company> listCompany = companyService.findAll();
		request.setAttribute("listCompany",listCompany);
		return "order/add";
	}
	
	/**查询订单列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		
		OrderService.OrderCriteria  criteria = new OrderService.OrderCriteria();
		String orderCode = request.getParameter("orderCode");
		if(StringUtils.isNotBlank(orderCode))
			criteria.orderCodeLike(orderCode);
		String mblCode = request.getParameter("mblCode");
		if(StringUtils.isNotBlank(mblCode))
			criteria.mblCodeLike(mblCode);
		String customerIdStr = request.getParameter("customerId");
		if(StringUtils.isNotBlank(customerIdStr))
			criteria.customerEqual(Integer.parseInt(customerIdStr));
		
		PaginationVo<Order> result = orderService.findList(criteria, currentPage, pageSize);
		request.setAttribute("result",result);
		
		/*客户列表*/
		List<Company> customers = companyService.findByType("zj");
		request.setAttribute("customers",customers);
		
		return "order/list";
	}
	
	/**获取单个订单*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model,HttpServletRequest request){
		Order order = orderService.getById(id);
		model.addAttribute("obj", order);
		return "order/edit";
	}
	
	/**更新订单*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Order order){
		Order _order = orderService.getById(order.getId());
		if(_order != null && !_order.getId().equals(order.getId()))
			return "{flag:1}";
		order.setUpdateDate(new Date());
		orderService.update(order);
		return "{flag:0}";
	}
	/**新增订单*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Order order,HttpServletResponse response,HttpServletRequest reuqest){
		order.setOrderCode(ToolUtil.getOrderCode());//arrange ordercode
		order.setInsertDate(new Date());
		User user = (User)reuqest.getSession().getAttribute("loginUser");
		order.setOperator(user);
		if(orderService.getByOrderCode(order.getOrderCode()) != null){
			return "{flag:1}";
		}
		orderService.add(order);
		return "{flag:0}";
	}
	/**删除订单*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		orderService.batchDelete(idIntArray);
		return "{success:true}";
	}
}
